//
//  SQStatusCellPictures.swift
//  SwiftWeibo
//
//  Created by 苏强 on 16/12/6.
//  Copyright © 2016年 suqianghotel. All rights reserved.
//

import UIKit

class SQStatusPicturesView: UIView {
    
    @IBOutlet weak var picturesViewHeightContraint: NSLayoutConstraint!
    
    var viewModel: SQStatusViewModel?{
        didSet{
            
            for view in subviews
            {
                view.isHidden = true
            }
            
            var idx = 0
            

            let rect = CGRect(origin: CGPoint(x: SQStatusCellOutterMargin, y:SQStatusCellOutterMargin),
                              size: CGSize(width:SQStatusPictureViewWidth, height:SQStatusPictureViewWidth))
            
            let picModels = viewModel?.picURLs ?? []
            for picModel: SQStatusPicture in picModels
            {
                
                if picModels.count == 4 && idx == 2
                {
                    idx += 1
                }
                
                let imageView: UIImageView = subviews[idx] as! UIImageView
        
                let row = idx / 3
                let column = idx % 3
                
                let offsetX = CGFloat(column) * SQStatusPictureViewWidth +  CGFloat(column) * SQStatusPictureViewInnerMargin
                let offsetY = CGFloat(row) * SQStatusPictureViewWidth + CGFloat(row) * SQStatusPictureViewInnerMargin
                
                imageView.frame = rect.offsetBy(dx: offsetX, dy: offsetY)
                
                imageView.sq_setImage(with: picModel.thumbnail_pic, placeholderImage: UIImage(named:"timeline_card_middle_background_highlighted"))
                
//                imageView.sd_setImage(with: URL(string:picModel.thumbnail_pic!)!, placeholderImage: UIImage(named:"timeline_card_middle_background_highlighted"))
                
                imageView.isHidden = false
                
                idx += 1
                if idx == 9 {
                    break
                }
            
            }
            
            
            print("pictureSize--->\(viewModel?.pictureSize)")
            
            picturesViewHeightContraint.constant = viewModel?.pictureSize.height ?? SQStatusCellOutterMargin
            
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        self.backgroundColor = nil
        
        // UIColor.white
        
        setupUI()
    }
    
    
    private func setupUI()
    {
        let count = 9
        
        for _ in 0..<count
        {
            let imageView = UIImageView()
            imageView.contentMode = .center
            imageView.isHidden = true
            imageView.clipsToBounds = true
            addSubview(imageView)
        }
        
        
    }

    

}
